package com.example.homework8.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;

import com.example.homework8.R;
import com.example.homework8.adapter.RecordAdapter2;
import com.example.homework8.bean.RecordInfo;
import com.example.homework8.database.RecordDBHelper;
import com.example.homework8.utils.SharedUtil;

import java.util.ArrayList;


public class SearchFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    public SearchFragment() {

    }


    public static SearchFragment newInstance(String param1, String param2) {
        SearchFragment fragment = new SearchFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    SearchView searchView;
    ListView resultlist ;
    ArrayList<RecordInfo> result;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_search, container, false);
        SearchView searchView = view.findViewById(R.id.searchview);
        ListView resulutviw =view.findViewById(R.id.resultlist);
        resulutviw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                SharedUtil.getIntance(getActivity()).writeShared("goods_id",""+result.get(i).rowid);
                getActivity().getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.id_content, new MoneyFragment(), null)
                        .addToBackStack(null)
                        .commit();
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                result = RecordDBHelper.getInstance(getActivity(), 1).query("name like '" + s + "%'");
                RecordAdapter2 adapter2 = new RecordAdapter2(result,getActivity());
                resulutviw.setAdapter(adapter2);
                adapter2.notifyDataSetChanged();
                return  true;
            }
        });

        return  view;
    }
}